/**
 * ReportGear(2011)
 */
package com.reportgear.designer.gui.component.combobox;

import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;

/**
 * 整型值下拉框
 * 
 * @version 1.0 2011-4-27
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class JIntComboBox extends JComboBox {
	/**
	 * UID
	 */
	private static final long serialVersionUID = -860756985746764845L;

	@SuppressWarnings("serial")
	public JIntComboBox() {
		setRenderer(new DefaultListCellRenderer() {
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
					boolean cellHasFocus) {
				super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

				if (value instanceof Integer) {
					setText(value + "  ");
				}

				return this;
			}
		});
	}

	public int getSelectedInt() {
		if (getSelectedItem() == null)
			return -1;
		return ((Integer) getSelectedItem()).intValue();
	}

	public void setSelectedInt(int index) {
		for (int i = 0; i < getItemCount(); ++i) {
			if (((Integer) getItemAt(i)).intValue() != index) {
				continue;
			}

			setSelectedIndex(i);
			return;
		}
	}
}